package icu.honeysuccess.gateway.filter;

import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.ServerHttpResponse;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author Honey
 * @date 2022/3/18
 */
@Component
public class IpFilter implements Ordered, GlobalFilter {
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain) {
        System.out.println("经过了第一个过滤器");
        ServerHttpRequest request = exchange.getRequest();
        ServerHttpResponse response = exchange.getResponse();
        // 获得主机名和ip地址
//        System.out.println(request.getRemoteAddress().getHostName());
//        System.out.println(request.getRemoteAddress().getHostString());
        // 放行请求
        return chain.filter(exchange);
    }

    @Override
    public int getOrder() {
        return 1;
    }
}
